/**
 * Created by victor on 2016/8/25.
 */
export const STORAGE_LOGIN = 'LoginDataxxxx12'
import Vue from 'vue'
const loginData = JSON.parse(localStorage.getItem(STORAGE_LOGIN))||{ count:0,users:[],currentUser:{ } }
const state ={
  count: loginData.count,
  users: loginData.users,
  currentUser: loginData.currentUser
}
const mutations={
  INCREMENT (state) {
    state.count++
  },
  DECREMENT (state) {
    state.count--
  },
  //登录 用户名密码是否正确 ,通过,就把该用户的权限置为 true.
  //设计上有缺陷诶,如果用户密码输的不正确,
  LOGIN_IN (state, currentUser) {
    var user = state.users.find(u => u.username == currentUser.username && u.password == currentUser.password )
    if(user){
      user.privilege = true
      state.currentUser = user
    }else{
      state.currentUser = { }
    }
  },
  //注册 ,直接添加到数组中,用户名重复的判断在之前就被控制,重复,就不允许点击.
  SIGN_UP (state, currentUser){
    var user = state.users.find(u=>u.username == currentUser.username)
    if(!user){
      state.users.push(currentUser)
      currentUser.privilege = true
      state.currentUser = currentUser
      // Vue.set(state,"currentUser",currentUser)
    }else{
      currentUser.privilege = false
      state.currentUser = currentUser
    }
  }
}
export default {
  state,
  mutations
}
